package com.datastructure.hashtab;
//根据关键码值而直接进行访问的数据结构，通过把关键码值映射到表中的一个位置来访问记录，以加快查找的速度。
//映射函数叫做散列函数，存放记录的数组叫做散列表
public class EmpLinkedList {
    private Emp head;

    public void add(Emp emp){
        if(head == null){
           head = emp;
           return;
        }
        Emp cur = head;
        while (true){
            if(cur.next == null){
                break;
            }
            cur = cur.next;
        }
        cur.next = emp;
    }

    public void addByOrder(Emp emp){
        Emp buffer=head;
        if(head == null){
            head = emp;
            return;
        }
        if (head.next==null){
            head.next=emp;
        } else {
            while (true){
                if(buffer.next==null){
                    break;
                }
                if(buffer.next.id> emp.id){
                    break;
                }
                buffer=buffer.next;
            }
            emp.next=buffer.next;
            buffer.next=emp;
        }
    }

    public void delete(Emp target){
        boolean flag = false;
        Emp cur = head;
        while (true){
            if(cur.next == null){
                break;
            }
            if(cur.next.id== target.id){
                flag = true;
                break;
            }
            cur = cur.next;
        }
        if(flag){
            if(cur.next.next!=null){
                cur.next=cur.next.next;
            }else {
                cur.next=null;
            }
        } else {
            System.out.printf("链表中没有该雇员");
        }
    }

    public void list(int i){
        if(head == null){
            System.out.printf("第%d条链表为空\n",i+1);
            return;
        }
        System.out.printf("第%d条链表信息为\n",i+1);
        Emp cur = head;
        while (true){
            if(cur == null){
                break;
            }
            System.out.printf(" => id=%d name=%s\t",cur.id,cur.name);
            cur=cur.next;
        }
    }

    public Emp findEmpById(int id){
        if(head == null){
            System.out.println("链表为空，没有数据～");
            return null;
        }
        Emp cur = head;
        while (true){
            if(cur.next == null){
                cur=null;
                break;
            }
            if(cur.id == id){
                break;
            }
            cur = cur.next;
        }
        return cur;
    }

}
